package com.briup.mt.service.impl;

import com.briup.mt.bean.Article;
import com.briup.mt.bean.ArticleExample;
import com.briup.mt.dao.ArticleMapper;
import com.briup.mt.service.ArticleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ArticleImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public List<Article> query(String title, Long publishTime) {
        ArticleExample articleExample = new ArticleExample();
        if(!StringUtils.isEmpty(title)){
            articleExample.createCriteria().andTitleLike("%"+title+"%");
        }
        if(!StringUtils.isEmpty(publishTime)){
                articleExample.createCriteria().andPublishTimeEqualTo(publishTime);
        }

        List<Article> list = articleMapper.selectByExample(articleExample);
        return list;


    }


    @Override
    public void saveOrUpdate(Article article) {
        if(article.getId() == null){
            articleMapper.insert(article);
        } else {
            articleMapper.updateByPrimaryKey(article);
        }
    }

    @Override
    public void deleteById(long id) {
        articleMapper.deleteByPrimaryKey(id);

    }
}
